﻿using Serilog;
using Serilog.Events;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// 配置Serilog
builder.Host.UseSerilog((hostBuilderContext, loggerConfigration) => {
    // 从配置文件中读取Serilog的配置
    // loggerConfigration.ReadFrom.Configuration(hostBuilderContext.Configuration)


    // 非配置文件方式
    // 不从配置文件加载Serilog配置
    loggerConfigration.WriteTo.Console()
    .WriteTo.File($"{builder.Environment.ContentRootPath}/logs/yubing.log",
                   rollingInterval: RollingInterval.Day, 
                   outputTemplate: "{Timestamp:HH:mm:ss fff zzz} " +  // 时间戳格式
                                   " || {Level} " +                    // Level:日志级别
                                " || {SourceContext:1} " +          //SourceContext：日志上下文
                                " || {Message} " +                  // Message：日志内容
                                " || {Exception} " +                // Exception：异常信息
                                " || end {NewLine}"                  //end:结束标志 NewLine：换行
                  )
    // 日志登记过滤
    .MinimumLevel.Information() // 设置最小级别
    .MinimumLevel.Override("Default", LogEventLevel.Information) // 默认设置
    .MinimumLevel.Override("Microsoft", LogEventLevel.Error) // 只输出微软的错误日志
    // 声明周期日志
    .MinimumLevel.Override("Default.Hosting.Lifetime", LogEventLevel.Information)
    .Enrich.FromLogContext();  // 将日志上下文也记录到日志中
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=SerilogTest}/{id?}");

app.Run();
